package ${PACKAGE_NAME};

#set($LOWER_NAME=${NAME.toLowerCase()})
import com.foresee.ems.utils.paging.PageCast;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ${USER} on ${DATE}.
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class ${NAME}Service {
    @Resource
    private ${NAME}Mapper ${LOWER_NAME}Mapper;

    @Resource
    private ${NAME}Dao ${LOWER_NAME}Dao;

    /**
     * 分页查询(Mybatis)
     *
     * @param ${LOWER_NAME} 查询参数
     * @param curPage  当前页码
     * @param pageSize 每页条数
     * @return
     */
    public PageInfo<${NAME}> findAll(${NAME} ${LOWER_NAME}, int curPage, int pageSize) {
        PageHelper.startPage(curPage, pageSize);
        List<${NAME}> all = ${LOWER_NAME}Mapper.findAll(${LOWER_NAME});
        return new PageInfo<${NAME}>(all);
    }

    /**
     * 分页查询(Jpa)
     *
     * @param searchParams 查询参数
     * @param curPage      当前页
     * @param pageSize     每页大小
     * @return
     */
    public PageInfo<${NAME}> findUser(Map<String, Object> searchParams, int curPage, int pageSize) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);//解析请求参数
        Specification<${NAME}> us = DynamicSpecifications.bySearchFilter(filters.values(), ${NAME}.class);//
        PageRequest rr = new PageRequest(curPage - 1, pageSize);
        Page<${NAME}> all = this.${LOWER_NAME}Dao.findAll(us, rr);
        return PageCast.cast2PageInfo(all);
    }

    /**
     * 根据id查询
     *
     * @param id 待查询的id
     * @return
     */
    public ${NAME} findById(String id) {
        return this.${LOWER_NAME}Dao.findOne(id);
    }

    /**
     * 保存
     *
     * @param ${LOWER_NAME}
     * @return
     */
    @Transactional
    public ${NAME} save(${NAME} ${LOWER_NAME}) {
        return this.${LOWER_NAME}Dao.save(${LOWER_NAME});
    }

    /**
     * 删除
     *
     * @param ${LOWER_NAME}
     */
    @Transactional
    public void delete(${NAME} ${LOWER_NAME}) {
        this.${LOWER_NAME}Dao.delete(${LOWER_NAME});
    }
}